package com.example.zhangxin.myhuatuob2b.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.zhangxin.myhuatuob2b.R;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by ZhangXin on 2016/10/13.
 */

public class RefreshListView extends ListView implements AbsListView.OnScrollListener {

    private final int PULL_REFRESH = 0;
    private final int RELRASE_REFRESH = 1;
    private final int REFRESHING = 2;
    private ImageView in_arrow;
    private TextView tv_title;
    private TextView tv_date;
    private int headerHeight;
    private int currentState = PULL_REFRESH;
    private int downY;
    private View headView;
    private RotateAnimation upAnimation;
    private RotateAnimation downAnimation;
    private ProgressBar pb_rotate;
    private int footerHeight;
    private View footerView;
    private boolean isLoadingMore = false;
    private onRefreshListener listener;

    public RefreshListView(Context context) {
        super(context);
        init();
    }

    public RefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setOnScrollListener(this);
        initHraderView();
        initFooterView();
        initAnimation();
    }

    private void initAnimation() {
        upAnimation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        upAnimation.setFillAfter(true);
        upAnimation.setDuration(300);

        downAnimation = new RotateAnimation(-180, -360, RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        downAnimation.setFillAfter(true);
        downAnimation.setDuration(300);
    }

    private void initFooterView() {
        footerView = View.inflate(getContext(), R.layout.lyaout_footer,null);
        //测量自己的高度
        footerView.measure(0, 0);
        footerHeight = footerView.getMeasuredHeight();
        footerView.setPadding(0, -footerHeight, 0, 0);
        addFooterView(footerView);
    }

    private void initHraderView() {
        headView = View.inflate(getContext(), R.layout.layout_head, null);
        pb_rotate = (ProgressBar) headView.findViewById(R.id.pb_rotate);
        in_arrow = (ImageView) headView.findViewById(R.id.iv_arrow);
        tv_title = (TextView) headView.findViewById(R.id.tv_title);
        tv_date = (TextView) headView.findViewById(R.id.tv_date);
        headView.measure(0, 0);
        headerHeight = headView.getMeasuredHeight();
        headView.setPadding(0, -headerHeight, 0, 0);
        addHeaderView(headView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaY = (int) (ev.getY() - downY);
                int paddingTop = -headerHeight + deltaY;
                if (paddingTop > -headerHeight && getFirstVisiblePosition() == 0) {
                    headView.setPadding(0, paddingTop, 0, 0);
                    if (paddingTop > 0 && currentState == PULL_REFRESH) {
                        currentState = RELRASE_REFRESH;
                        refreshHeaderView();
                    } else if (paddingTop < 0 && currentState == RELRASE_REFRESH) {
                        currentState = PULL_REFRESH;
                        refreshHeaderView();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                if(currentState == PULL_REFRESH){
                    headView.setPadding(0,-headerHeight,0,0);
                }else if(currentState == RELRASE_REFRESH){
                    headView.setPadding(0,0,0,0);
                    currentState = REFRESHING;
                    refreshHeaderView();
                    if(listener!=null){
                        listener.onPullRefresh();
                    }
                }

                break;
        }
        return super.onTouchEvent(ev);
    }

    private void refreshHeaderView() {
        switch (currentState) {
            case PULL_REFRESH:
                tv_title.setText("下拉刷新");
                in_arrow.startAnimation(downAnimation);
                break;
            case RELRASE_REFRESH:
                tv_title.setText("松开刷新");
                in_arrow.startAnimation(upAnimation);
                break;
            case REFRESHING:
                tv_title.setText("正在刷新...");
                in_arrow.setVisibility(INVISIBLE);
                in_arrow.clearAnimation();
                pb_rotate.setVisibility(VISIBLE);
                break;
        }
    }

    public void comLetepefresh(){
        //重置
        if(isLoadingMore){
            footerView.setPadding(0,-footerHeight,0,0);
            isLoadingMore = false;
        }else{
            headView.setPadding(0,-headerHeight,0,0);
            currentState = PULL_REFRESH;
            tv_title.setText("下拉刷新");
            pb_rotate.setVisibility(INVISIBLE);
            in_arrow.setVisibility(VISIBLE);
            tv_date.setText("最后刷新:"+getCurrentDate());
        }
    }

    private String getCurrentDate(){
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH-mm-ss");

        return format.format(new Date());
    }

    //监听滑动
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        System.out.println(scrollState+"+++++++++++++++++++++++++++");
        if(scrollState == OnScrollListener.SCROLL_STATE_IDLE
                &&getLastVisiblePosition()==(getCount()-1)&&!isLoadingMore){
            isLoadingMore = true;
            footerView.setPadding(0,0,0,0);
            //把指定条目放在listview最上面
            setSelection(getCount());

            if(listener!=null){
                listener.onLoadingMore();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    public void setOnRefreshListener(onRefreshListener listener){
        this.listener = listener;
    }



    public  interface onRefreshListener{
        void onPullRefresh();
        void onLoadingMore();
    }

}
